package com.shigen.shop.controller;

import com.shigen.shop.comon.Result;
import com.shigen.shop.req.AddressReq.AddAddressReq;
import com.shigen.shop.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 地址控制类
 *
 * @author shigenfu
 * @date 2022/7/24 3:12 下午
 */
@RestController
@Api(tags = "地址控制类")
@RequestMapping(value = "address")
public class AddressController {

  @Resource
  private AddressService addressService;

  @GetMapping(value = "get/{userId}")
  @ApiOperation(value = "根据userId获得地址")
  public Result getAddressByUserId(@PathVariable("userId") Long userId) {
    return Result.success(addressService.getAddressByUserId(userId));
  }

  @DeleteMapping(value = "delete/{userId}")
  @ApiOperation(value = "根据userId删除地址")
  public Result deleteAddressByUserId(@PathVariable("userId") Long userId) {
    return addressService.deleteAddressByUserId(userId) > 0 ? Result.success(null) : Result.err(null);
  }

  @PostMapping(value = "add")
  @ApiOperation(value = "增加用户收货地址")
  public Result addAddress(@RequestBody @Valid AddAddressReq addressReq) {
    return addressService.addAddress(addressReq) > 0 ? Result.success(null) : Result.err(null);
  }
}
